<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>

    /* 
        let goods = [
          { id: 1, name: '香蕉1' },
          { id: 2, name: '香蕉2' },
          { id: 3, name: '香蕉3' },
          { id: 4, name: '香蕉4' },
          { id: 5, name: '香蕉5' },
          { id: 6, name: '香蕉6' },
        ]
    
        // JSON.stringify(goods),转换为字符串，栈里面做缓存
        // JSON.parse（）， 转为对象， 堆里面重新存储。新开辟一个内存地址。
        let arr2 = JSON.parse(JSON.stringify(goods))
    
        arr2[0].name = "aaaaaaaa"
        console.log(arr2);
        console.log(goods);
    
     */

    let obj = { info: { title: 'xxx', content: 'aaaa' }, a: 1, b: undefined, say: function () { console.log('hello'); } }

    // let newObj = JSON.parse(JSON.stringify(obj))

    // obj.b = "xxxx"
    // console.log(newObj);

    // 使用递归函数，来反复调用函数自身。
    function deepCopy(obj) {
      let o = {}
      for (let key in obj) {
        console.log(key); // a, b, say
        // o.key = obj.key;
        // 给O对象，动态的添加属性和方法
        // 如果对象的属性时引用类型，则递归调用这个函数自己。
        if (typeof obj[key] === 'object') {
          // obj.info = deepCopy(obj.info)
          o[key] = deepCopy(obj[key]) // deepCopy(obj[key]) 里面的逻辑执行结束后，返回一个新对象给左边的变量。
        } else {
          o[key] = obj[key]
        }
      }
      return o;
    }

    let newObj = deepCopy(obj)

    console.log(newObj);

    obj.info.title = "yuyyy"


    // 比如说二维数组，数组里面的元素时对象

    let goods = [
      [{ id: 1, name: '香蕉1' },
      { id: 2, name: '香蕉2' },
      { id: 3, name: '香蕉3' },
      { id: 4, name: '香蕉4' },
      { id: 5, name: '香蕉5' },
      { id: 6, name: '香蕉6' },
      ],
      [{ id: 1, name: '香蕉1' },
      { id: 2, name: '香蕉2' },
      { id: 3, name: '香蕉3' },
      { id: 4, name: '香蕉4' },
      { id: 5, name: '香蕉5' },
      { id: 6, name: '香蕉6' },
      ],
      [
        { id: 1, name: '香蕉1' },
        { id: 2, name: '香蕉2' },
        { id: 3, name: '香蕉3' },
        { id: 4, name: '香蕉4' },
        { id: 5, name: '香蕉5' },
        { id: 6, name: '香蕉6' },
      ]]

    let arr = [];
    goods.forEach(el => {
      let temp = [];
      el.forEach(item => {
        let obj = {};
        for (let key in item) {
          obj[key] = item[key]
        }
        temp.push(obj)
      })

      arr.push(temp)
    });
    console.log(goods);
    console.log(arr);

    arr[0][0].name = "苹果"


  </script>
</body>

</html>